#include<main.h>

//#define DEBUG

#define OPT_SEN_COUNT_MAX 999999 //计数最大值
#define OPT_SEN_CHECK_INT 10 //抗干扰计数参数
#define OPT_SEN_TIMEOUT 250 //自动任务超时*20ms

#define TOUCH_KEY_CHECK_INT 25 //按钮抗干扰计数参数


unsigned long OPT_SEN_Count=0; //计数值
unsigned long OPT_SEN_Count_ago=0; //计数值

unsigned int OPT_SEN_On_cnt=0; //抗干扰计数值
unsigned int OPT_SEN_Off_cnt=0;//抗干扰计数值

unsigned int OPT_SEN_TimeOut=0;//超时值


unsigned int TouchKey1_Press_cnt=0;//抗干扰计数值
unsigned int TouchKey2_Press_cnt=0;//抗干扰计数值
unsigned int TouchKey3_Press_cnt=0;//抗干扰计数值
unsigned int TouchKey4_Press_cnt=0;//抗干扰计数值

unsigned int CurrKey_Press_cnt=0;//抗干扰计数值
unsigned int CurrCheckDelay20ms_cnt=0;//电流检测延时
bit RelayStateAgo=0; //用于继电器上升沿检测


unsigned char errcode_display_do=0;
unsigned char errcode_display_ago=0;

bit lan_cs=1;
bit lan_cs_ago=0;

bit OPT_SEN_state_ago=0; //前一次状态
bit OPT_SEN_state=0; //当前状态
bit OPT_SEN_TurnOn_Event=0; //上升沿事件
bit OPT_SEN_TurnOff_Event=0; //下降沿事件

bit RELAY_CheckStop_En=0; //继电器断开使能,用于启动时不马上根据OPT_SEN状态停机,在上升下降沿事件处理中自动使能
bit RELAY_Auto_En=0; //
bit RELAY_Auto_En_ago=0; //
bit RELAY_Manual_En=0;
bit RELAY_Manual_En_ago=0;


bit TouchKey1_press_down=0;
bit TouchKey2_press_down=0;
bit TouchKey3_press_down=0;
bit TouchKey4_press_down=0;

bit TouchKey1_press_up=0;
bit TouchKey2_press_up=0;
bit TouchKey3_press_up=0;
bit TouchKey4_press_up=0;

bit TouchKey1_pressed=0;
bit TouchKey2_pressed=0;
bit TouchKey3_pressed=0;
bit TouchKey4_pressed=0;
	
bit TouchKey1_press=0;
bit TouchKey2_press=0;
bit TouchKey3_press=0;
bit TouchKey4_press=0;

bit TouchKey1_pressed_ago=0;
//bit TouchKey2_pressed_ago=0;
bit TouchKey3_pressed_ago=0;
	
	
u16 touchX=0;
u16 touchY=0;
	u8 disint[7]={'0','0','0','0','0','0',0};
	


	
//void delay1s(unsigned int dcnt1);
//void delayms(unsigned int dcnt);
void lcd_disNumber(u16 x,u16 y,u16 width,u16 height,unsigned long number);
void disButton1(u8 isPressed);
void disButton2(u8 isPressed);
	
void Touch_Key_Check(void);
void Touch_Key_Display(void);

void OPT_SEN_IO_Check(void);
void OPT_SEN_IO_PROCESS(void);
void RELAY_Work(void);
	
	
	
void Timer0Init(void)		//20毫秒@33.1776MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x00;		//设置定时初值
	TH0 = 0x28;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	
    ET0 = 1;                                    //使能定时器中断
    EA = 1;
}

	
//void delayms(unsigned int dcnt)
//{
//    while(dcnt > 0) dcnt -- ;
//}


//void delay1s(unsigned int dcnt1)
//{
//    while(dcnt1 > 0)
//    {
//    delayms(5000);
//    dcnt1 -- ;
//    }
//}
void lcd_disNumber(u16 x,u16 y,u16 width,u16 height,unsigned long number)
{
	disint[0]=0x30+(number/100000)%10;
	disint[1]=0x30+(number/10000)%10;
	disint[2]=0x30+(number/1000)%10;
	disint[3]=0x30+(number/100)%10;
	disint[4]=0x30+(number/10)%10;
	disint[5]=0x30+(number)%10;
			LCD_CS = 0;
	LCD_Fill(x,y,x+width,y+height,WHITE);
	LCD_ShowString(x,y,width,height,disint,RED,0);
			LCD_CS = 1;
}

//void disNumber(unsigned long num1,unsigned long num2)
//{
//POINT_COLOR = RED;
////lcd_disNumber(50,40,16*6,32,num1);
////lcd_disNumber(50+40+(16*6),40,16*6,32,num2);
//lcd_disNumber(50,40,16*6,32,num1);
//lcd_disNumber(50+40+(16*6),40,16*6,32,num2);
//}



void KEYS_PROCESS(void)
{
	//自动启动一次
	if(TouchKey1_pressed)
	{
		if(0==RELAY_Auto_En)
		{
			RELAY_Auto_En=1;
			errcode_display_do=0;
		}
		else
		{
			RELAY_Auto_En=0;
		}
		
		TouchKey1_pressed=0;
	}
	
	//
//	if(TouchKey2_pressed)
//	{
//		
//		TouchKey2_pressed=0;
//	}
//	
	if(TouchKey3_pressed)
	{
		
		TouchKey3_pressed=0;
		OPT_SEN_Count=0;
	}
	
	//手动点动控制
	if(TouchKey2_press&&TouchKey2_pressed)
	{
		TouchKey2_pressed=0;
		RELAY_Manual_En=1;
	}
	else if(!TouchKey2_press)//&&TouchKey2_press_up)
	{
		TouchKey2_press_up=0;
		RELAY_Manual_En=0;
	}
	
	//语言切换
	if(TouchKey4_pressed)
	{
		if(0==lan_cs) lan_cs=1; else lan_cs=0;
		TouchKey4_pressed=0;
	}
	
	
	
}

void Touch_Key_Display(void)
{
	
	if(RELAY_Auto_En!=RELAY_Auto_En_ago)
	{
		buttonDISPcolorchg(50,190,200,290,GREEN,CYAN,RELAY_Auto_En);
		
		
		
		
		if(0==lan_cs)
		{
			LCD_ShowString(95,220,16*4,32,"AUTO",BLACK,0); //自动
		}
		else if(1==lan_cs)
		{
			LCD_ShowString(95,220,32*2,32,";1",BLACK,1); //自动
		}
		RELAY_Auto_En_ago=RELAY_Auto_En;
	}

	if(RELAY_Manual_En!=RELAY_Manual_En_ago)
	{
		buttonDISP(280,190,430,290,YELLOW,RELAY_Manual_En);

		if(0==lan_cs)
		{
			LCD_ShowString(322+8,220,16*3,32,"JOG",BLACK,0); //点动
		}
		else if(1==lan_cs)
		{
			LCD_ShowString(322,220,32*2,32,"21",BLACK,1); //点动
		}
		RELAY_Manual_En_ago=RELAY_Manual_En;
	}

	if(TouchKey3_pressed!=TouchKey3_pressed_ago)
	{
		buttonDISP(317,100,393,150,RED,TouchKey3_pressed);

		if(0==lan_cs)
		{
			LCD_ShowString(324+8,110,16*3,32,"CLR",BLACK,0); //清零
		}
		else if(1==lan_cs)
		{
			LCD_ShowString(324,110,32*2,32,"34",BLACK,1); //清零
		}
		TouchKey3_pressed_ago=TouchKey3_pressed;
	}
}


//触屏按钮检测
void Touch_Key_Check(void)
{
			SPCTL = 0xdc;                             
			SPSTAT = 0xc0;        //清中断标志
			touchX=ADS_Read_AD(0X90);
			touchY=ADS_Read_AD(0XD0);
			
		if(touchY>240&&touchY<880)
		{
				//按钮1
				if(touchX>350&&touchX<1024)
				{
					if(0==TouchKey1_press) { TouchKey1_press_down=1; TouchKey1_press=1; }
					
					if(TouchKey1_Press_cnt<TOUCH_KEY_CHECK_INT)
					{
						TouchKey1_Press_cnt++;
						if(TOUCH_KEY_CHECK_INT==TouchKey1_Press_cnt) TouchKey1_pressed=1;
					}
				 TouchKey2_press=0;
				 TouchKey2_Press_cnt=0;
				}
				
				//按钮2
				if(touchX>1248&&touchX<1872)
				{	
					if(0==TouchKey2_press) { TouchKey2_press_down=1; TouchKey2_press=1; }
					if(TouchKey2_Press_cnt<TOUCH_KEY_CHECK_INT)
					{
						TouchKey2_Press_cnt++;
						if(TOUCH_KEY_CHECK_INT==TouchKey2_Press_cnt) TouchKey2_pressed=1;
					}
					TouchKey1_press=0;
					TouchKey1_Press_cnt=0;
				}

     TouchKey3_press=0;
     TouchKey3_Press_cnt=0;
		 TouchKey4_press=0;
     TouchKey4_Press_cnt=0;		

		}
		else if(touchY>1104&&touchY<1376)
		{
    //??3
    if(touchX>1380&&touchX<1744)
    { 
     if(0==TouchKey3_press) { TouchKey3_press_down=1; TouchKey3_press=1; }
     if(TouchKey3_Press_cnt<TOUCH_KEY_CHECK_INT)
     {
      TouchKey3_Press_cnt++;
      if(TOUCH_KEY_CHECK_INT==TouchKey3_Press_cnt) TouchKey3_pressed=1;
     }
    }
		
		 TouchKey1_press=0;
     TouchKey1_Press_cnt=0;
     TouchKey2_press=0;
     TouchKey2_Press_cnt=0;
     TouchKey4_press=0;
     TouchKey4_Press_cnt=0;		
   }
		else if(touchY>1632&&touchY<1872)
		{
    //??3
    if(touchX>230&&touchX<448)
    { 
     if(0==TouchKey4_press) { TouchKey4_press_down=1; TouchKey4_press=1; }
     if(TouchKey4_Press_cnt<TOUCH_KEY_CHECK_INT)
     {
      TouchKey4_Press_cnt++;
      if(TOUCH_KEY_CHECK_INT==TouchKey4_Press_cnt) TouchKey4_pressed=1;
     }
    }
		
		 TouchKey1_press=0;
     TouchKey1_Press_cnt=0;
     TouchKey2_press=0;
     TouchKey2_Press_cnt=0;
		 TouchKey3_press=0;
     TouchKey3_Press_cnt=0;
   }
		
   else
   {
		 if(1==TouchKey1_press) { TouchKey1_press_up=1; }
		 if(1==TouchKey2_press) { TouchKey2_press_up=1; }
		 if(1==TouchKey3_press) { TouchKey3_press_up=1; }		
		 if(1==TouchKey4_press) { TouchKey4_press_up=1; }		
		 
     TouchKey1_press=0;
     TouchKey1_Press_cnt=0;
     TouchKey2_press=0;
     TouchKey2_Press_cnt=0;
     TouchKey3_press=0;
     TouchKey3_Press_cnt=0;
     TouchKey4_press=0;
     TouchKey4_Press_cnt=0;	 
   }
//			else
//			{
//					TouchKey1_press=0;
//					TouchKey1_Press_cnt=0;
//					TouchKey2_press=0;
//					TouchKey2_Press_cnt=0;
//			}
			
}


//触屏坐标显示
void Touch_XY_Display(void)
{
	//计数显示
	if(OPT_SEN_Count!=OPT_SEN_Count_ago)
	{
		OPT_SEN_Count_ago=OPT_SEN_Count;
		SPCTL = 0xd0;                             
		SPSTAT = 0xc0;        //清中断标志
		lcd_disNumber(80,92+32,16*6,32,OPT_SEN_Count);
		
		IapEraseSector(0x0000);			
		IapProgramByte(0x0000, (OPT_SEN_Count>>24)%0x100);
		IapProgramByte(0x0001, (OPT_SEN_Count>>16)%0x100);
		IapProgramByte(0x0002, (OPT_SEN_Count>>8)%0x100);
		IapProgramByte(0x0003, (OPT_SEN_Count%0x100));
		
	}
	
	//错误码显示
	if(errcode_display_do!=errcode_display_ago)
	{
		LCD_CS = 0;
		LCD_Fill(415-5,25,415+5+(16*3),25+32,RED);
		if(1==errcode_display_do)
			LCD_ShowString(415,25,16*3,32,"E01",WHITE,0);
		else if(2==errcode_display_do)
			LCD_ShowString(415,25,16*3,32,"E02",WHITE,0);
		else
			LCD_Fill(415-5,25,415+5+(16*3),25+32,WHITE);
		
		LCD_CS = 1;
		//lcd_disNumber(400+16,25,16*2,32,errcode_display_do);
		errcode_display_ago=errcode_display_do;
	}
	
	//语言显示
	if(lan_cs!=lan_cs_ago)
	{
		LCD_CS = 0;
		LCD_Fill(25-5,25,25+5+(16*2),25+32,BLUE);
		LCD_Fill(80,25,80+(16*18),25+32,WHITE);
		LCD_Fill(80,92,80+(16*5),92+32,WHITE);
		buttonDISP(317,100,393,150,RED,0);
		buttonDISPcolorchg(50,190,200,290,GREEN,CYAN,RELAY_Auto_En);
		//buttonDISP(50,190,200,290,GREEN,0);
		buttonDISP(280,190,430,290,YELLOW,0);
		
		
		if(0==lan_cs)
		{
			LCD_ShowString(25,25,16*2,32,"EN",WHITE,0);
			
			
			
			
			
			
			
			
			LCD_ShowString(80,25,16*18,32,"INTELLIGENT SEALER",BLACK,0);	
			LCD_ShowString(80,92,16*5,32,"COUNT",BLACK,0);
			LCD_ShowString(95,220,16*4,32,"AUTO",BLACK,0); //自动
			LCD_ShowString(322+8,220,16*3,32,"JOG",BLACK,0); //点动
			LCD_ShowString(324+8,110,16*3,32,"CLR",BLACK,0); //清零
		}
		else if(1==lan_cs)
		{
			LCD_ShowString(25,25,16*2,32,"CH",WHITE,0);
			LCD_ShowString(150,25,32*6,64,"50189:",BLACK,1); //自动封口机
			LCD_ShowString(80,92,32*2,32,"67",BLACK,1);//计数			
			LCD_ShowString(95,220,32*2,32,";1",BLACK,1); //自动
			LCD_ShowString(322,220,32*2,32,"21",BLACK,1); //点动
			LCD_ShowString(324,110,32*2,32,"34",BLACK,1); //清零
		}
		
		IapEraseSector(0x0201);			
		IapProgramByte(0x0201, lan_cs);

		LCD_CS = 1;
		//lcd_disNumber(400+16,25,16*2,32,errcode_display_do);
		lan_cs_ago=lan_cs;
	}

	#ifdef DEBUG
	SPCTL = 0xd0;                             
	SPSTAT = 0xc0;        //清中断标志
	lcd_disNumber(110,0,16*6,32,touchX);
	lcd_disNumber(110+40+(16*6),0,16*6,32,touchY);
	#endif

}



//操作
void RELAY_Work(void)
{
	if(RELAY_Auto_En)
	{
		if(0==REALY)
		{
			REALY=1;
			OPT_SEN_On_cnt=0;
			OPT_SEN_Off_cnt=0;
			OPT_SEN_TurnOn_Event=0;
			OPT_SEN_TurnOff_Event=0;
			RELAY_CheckStop_En=0;
			//超时检测初始化
			OPT_SEN_TimeOut=OPT_SEN_TIMEOUT;
		}
		OPT_SEN_IO_Check();
		OPT_SEN_IO_PROCESS();
		RELAY_CheckStop_En=1;
		
		//工作超时处理
//		if(OPT_SEN_TimeOut>0)
//		{OPT_SEN_TimeOut--;}
//		else
//		{
			if (0==OPT_SEN_TimeOut) {RELAY_Auto_En=0; errcode_display_do=1;}
//		}

	}
	else if(RELAY_Manual_En)
	{
		REALY=1;
	}
	else
	{
		REALY=0;
	}
	
	
	if(1==REALY)
	{
		if(0==RelayStateAgo) CurrCheckDelay20ms_cnt=25; //电机启动(Relay上升沿)延时500ms检测电流
		if(0==CurrCheckDelay20ms_cnt&&CMPCR1 & 0x01)//==AN_AUTO)
		{
			if(CurrKey_Press_cnt<10) { CurrKey_Press_cnt++; } else { RELAY_Auto_En=0; RELAY_Manual_En=0; errcode_display_do=2;}
		}
	}
	else
	{
		CurrKey_Press_cnt=0;
	}
	
	RelayStateAgo=REALY; //更新继电器标志位，用于Relay上升沿检测

}



//OPT_SEN管脚状态搞干扰检测
void OPT_SEN_IO_Check(void)
{
	if(1==OPT_SEN)
	{
		//管脚为1时开始on方向抗干扰计数，计数超过100后状态置1
		if (OPT_SEN_On_cnt<OPT_SEN_CHECK_INT)
		{
			OPT_SEN_On_cnt++; 
			if(OPT_SEN_CHECK_INT==OPT_SEN_On_cnt) OPT_SEN_state=1;
		}
		//清零off方向抗干扰计数
		OPT_SEN_Off_cnt=0;
	}
	else
	{
		//管脚为1时开始off方向抗干扰计数，计数超过100后状态置0
		if (OPT_SEN_Off_cnt<OPT_SEN_CHECK_INT) 
		{
			OPT_SEN_Off_cnt++; 
			if(OPT_SEN_CHECK_INT==OPT_SEN_Off_cnt) OPT_SEN_state=0;
		}
		//清零on方向抗干扰计数
		OPT_SEN_On_cnt=0;
	}
	
	//状态处理,产生上升沿和下降沿事件
	if(OPT_SEN_state!=OPT_SEN_state_ago)
	{
		if(OPT_SEN_state) OPT_SEN_TurnOn_Event=1; else OPT_SEN_TurnOff_Event=1;
		OPT_SEN_state_ago=OPT_SEN_state;
	}
	
}

//OPT_SEN管脚事件处理
void OPT_SEN_IO_PROCESS(void)
{
	//事件处理,上升沿
	if(OPT_SEN_TurnOn_Event)
	{
		if(OPT_SEN_Count<OPT_SEN_COUNT_MAX) OPT_SEN_Count++; //计数-上升沿计数
		if(RELAY_CheckStop_En) RELAY_Auto_En=0;//auto停机-上升沿
		OPT_SEN_TurnOn_Event=0;
	}
	//事件处理,下降沿
	if(OPT_SEN_TurnOff_Event)
	{
		//if(OPT_SEN_Count<OPT_SEN_COUNT_MAX) OPT_SEN_Count++;
		//RELAY_Auto_En=0;//auto停机-下降沿
		OPT_SEN_TurnOff_Event=0;
	}
}





void main(void)
{

		mcu_port_init();
	
		Timer0Init();

	
		LCD_CS = 0;
		LCD_Init();		
	
		LCD_Clear(WHITE);
		

	
	//恢复计数值
		OPT_SEN_Count=0;
		OPT_SEN_Count+=IapReadByte(0x0000);
		OPT_SEN_Count=OPT_SEN_Count<<8;
		OPT_SEN_Count+=IapReadByte(0x0001);
		OPT_SEN_Count=OPT_SEN_Count<<8;
		OPT_SEN_Count+=IapReadByte(0x0002);
		OPT_SEN_Count=OPT_SEN_Count<<8;
		OPT_SEN_Count+=IapReadByte(0x0003);
		
		if(OPT_SEN_Count>OPT_SEN_COUNT_MAX) OPT_SEN_Count=OPT_SEN_COUNT_MAX;
		OPT_SEN_Count_ago=OPT_SEN_Count;
		

		
		
		
		//恢复语言设置
		if(0x01==IapReadByte(0x0201)) lan_cs=1; else lan_cs=0;
		lan_cs_ago=lan_cs;
		
		LCD_CS = 0;
		LCD_Fill(25-5,25,25+5+(16*2),25+32,BLUE);
		buttonDISP(317,100,393,150,RED,0);
				buttonDISPcolorchg(50,190,200,290,GREEN,CYAN,0);
		buttonDISP(280,190,430,290,YELLOW,0);
		
		if(0==lan_cs)
		{
			LCD_ShowString(80,25,16*18,32,"INTELLIGENT SEALER",BLACK,0);	
			LCD_ShowString(80,92,16*5,32,"COUNT",BLACK,0);
			LCD_ShowString(25,25,16*2,32,"EN",WHITE,0);
			LCD_ShowString(95,220,16*4,32,"AUTO",BLACK,0); //自动
			LCD_ShowString(322+8,220,16*3,32,"JOG",BLACK,0); //点动
			LCD_ShowString(324+8,110,16*3,32,"CLR",BLACK,0); //清零
		}
		else if(1==lan_cs)
		{
			LCD_ShowString(25,25,16*2,32,"CH",WHITE,0);
			LCD_ShowString(150,25,32*6,64,"50189:",BLACK,1); //自动封口机
			LCD_ShowString(80,92,32*2,32,"67",BLACK,1);//计数			
			LCD_ShowString(95,220,32*2,32,";1",BLACK,1); //自动
			LCD_ShowString(322,220,32*2,32,"21",BLACK,1); //点动
			LCD_ShowString(324,110,32*2,32,"34",BLACK,1); //清零
		}
		
		lcd_disNumber(80,92+32,16*6,32,OPT_SEN_Count);
		
		LCD_CS = 1; 
		
		
		
		LCD_BL = 1;
		
		PwmInit();
		SetLedPwm(50); //设置LED背光亮度 最大为999
		
		
    CMPCR2 = 0x00;
    CMPCR2 &= ~0x80;                            //比较器正向输出
//  CMPCR2 |= 0x80;                             //比较器反向输出
    CMPCR2 &= ~0x40;                            //禁止0.1us滤波
//  CMPCR2 |= 0x40;                             //使能0.1us滤波
//  CMPCR2 &= ~0x3f;                            //比较器结果直接输出
    CMPCR2 |= 0x10;                             //比较器结果经过16个去抖时钟后输出
		
		
    CMPCR1 = 0x00;

    CMPCR1 &= ~0x08;                            //P3.7为CMP+输入脚

    CMPCR1 |= 0x80;                             //使能比较器模块
		
		
		while(1)
		{
//			lan_cs=OPT_SEN;
			

			
			Touch_Key_Check();
			Touch_Key_Display();
			
			Touch_XY_Display();
	
			KEYS_PROCESS();
		  RELAY_Work();
//		OPT_SEN_IO_Check();
//		OPT_SEN_IO_PROCESS();
//			OPT_SEN_Count=OPT_SEN_TimeOut;///10;
//		delay1s(1);
		WDT_CONTR |= 0x10;                      //清看门狗,否则系统复位
		}
	
	
			}
			
void tm0_isr() interrupt 1
{
    if(OPT_SEN_TimeOut>0) OPT_SEN_TimeOut--;
		if(CurrCheckDelay20ms_cnt>0) CurrCheckDelay20ms_cnt--;
	
}			
			
			
		